package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    //注入OrderSettingDao
    @Autowired
    private OrderSettingDao orderSettingDao;

    //注入本地dao
    @Autowired
    private OrderDao orderDao;

    //注入会员dao
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
        //1.检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate"); //获取用户预约的日期
        Date date = DateUtils.parseString2Date(orderDate); //格式化日期
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);

        if (orderSetting == null) {
            //等于null,表示当前日期没有预约设置
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.检查用户所选择的预约日期是否已经约满,如果约满则不能预约
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations >= number) {
            //已经约满,不能预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //获取当前预约的手机号
        String telephone = (String) map.get("telephone");
        //根据手机号查询当前用户的是否为会员情况
        Member member = memberDao.findByTelephone(telephone);

        if (member != null) {

            //创建Order对象
            Order order = new Order(
                    member.getId(),
                    date,
                    (String) map.get("orderType"),
                    Order.ORDERSTATUS_NO,
                    Integer.parseInt((String) map.get("setmealId"))
            );
            //当前用户是会员,检查用户是否重复预约(同一个用户在同一天预约了同一个套餐),如果是重复预约则无法完成再次预约
            List<Order> list = orderDao.findByCondition(order);
            //判断集合是否不为空
            if (list != null && list.size() > 0) {
                //说明当前用户已经预约了当前套餐,提示用户不能重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //当前用户不是会员,则自动完成注册并进行预约
            //创建Member对象
            member = new Member();
            member.setName((String) map.get("name"));//会员名称
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            //调用dao层
            memberDao.add(member);
        }

        //保存信息到预约表
        Order order = new Order(
                member.getId(),
                date,
                (String) map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId"))
        );
        //5.完成预约,调用OrderDao
        orderDao.add(order);
        //更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        //调用OrderSettingDao
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    //查询体检预约成功后的信息
    @Override
    public Map findById4Detail(int id) {
        return orderDao.findById4Detail(id);
    }
}
